package cn.kgc.tangcco.dao.impl;

import cn.kgc.tangcco.dao.DujitangMongoDao;
import cn.kgc.tangcco.model.LayPage;
import cn.kgc.tangcco.pojo.Dujitang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 李昊哲
 * @Description
 * @create 2020/12/29 15:41
 */
@Component
public class DujitangMongoDaoImpl implements DujitangMongoDao {
    @Autowired
    private MongoOperations mongoOperations;


    @Resource
    MongoTemplate mongoTemplate;

    @Override
    public List<Dujitang> findAll() {
        return mongoTemplate.findAll(Dujitang.class);
    }

    @Override
    public void saveAll(List<Dujitang> dujitangs) {
        mongoTemplate.insertAll(dujitangs);
    }

    @Override
    public Long selectCount() {
        return mongoTemplate.count(new Query(), Dujitang.class);
    }

    @Override
    public List<Dujitang> select(int page, int limit) {
        Query query = new Query();
        Pageable pageable = PageRequest.of(page, limit);
        query.with(pageable);
        return mongoOperations.find(query, Dujitang.class);
    }
}
